package com.laoxu.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 登录控制器
 *
 * @author xusucheng
 * @create 2019-01-13
 **/
@Controller
public class LoginController {
    @PostMapping("/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map<String,Object> map,
                        HttpSession session
                        ){
        if(!StringUtils.isEmpty(username) && "123456".equals(password))
        {
            //登录成功
            session.setAttribute("loginUser",username);
            //防止重复提交
            return "redirect:/main.html";
        }
        else{
            map.put("msg","用户名或密码错误！");
            return "login";
        }

    }

    @RequestMapping(value = "/user/getMessage")
    @ResponseBody
    public String saveOrUpdate(HttpServletRequest request) {
       String username = request.getParameter("username");
       String age = request.getParameter("age");


        return "Hello "+username+"!";

    }
}
